<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\activity;

class CouponsStore extends \think\Model {

    protected $table = 'coupons_store';
    protected $connection = ['database' => 'activity'];

    /**
     * 构造方法
     * @param type $data
     */
    public function __construct($data=[]) {
        parent::__construct($data);
    }
    
    /**
     * 保存卡券和门店的关系
     * @param type $couponsId  卡券 
     * @param type $stores 门店的id数组
     */
    public function saveCouponsStore($couponsId, $stores){
        //把此卡券相关的信息删除掉
        $res = $this->where(['coupons_id'=>$couponsId])->delete();
        if($res === false){
            return false;
        }
        
        $insertArr = [];
        foreach($stores as $storeId){
            $insertArr[] = ['coupons_id' => $couponsId, 'store_id' => $storeId];
        }
        
        return $this->insertAll($insertArr);
    }
    
    
    /**
     * 根据优惠券ID获取适用品牌(For 后台)
     *
     * @return $couponsId
     */
    public function getStoreListByCouponsId($couponsId)
    {

        $info = $this->where(['coupons_id'=> $couponsId])->column('*');
        
        $storeidList = array_column($info, 'store_id'); 
        
        return implode(',',$storeidList);
    }
    
        /**
     * 根据优惠券ID获取适用品牌(For 后台)
     *
     * @return $couponsId
     */
    public function getStoreArrByCouponsId($couponsId)
    {

        $info = $this->where(['coupons_id'=> $couponsId])->column('*');
        
        $storeidList = array_column($info, 'store_id');
        
        return $storeidList;
    }
}